package cn.jksoft.businessclient.ui.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jksoft.businessclient.R;
import cn.jksoft.businessclient.api.ReceivingApi;
import cn.jksoft.businessclient.api.ReceivingService;
import cn.jksoft.businessclient.base.BaseApplication;
import cn.jksoft.businessclient.base.BaseFragment;
import cn.jksoft.businessclient.bean.BaseEntity;
import cn.jksoft.businessclient.bean.ProductEntity;
import cn.jksoft.businessclient.bean.StoreServiceEntity;
import cn.jksoft.businessclient.bean.WorkBenchEntity;
import cn.jksoft.businessclient.ui.adapter.ServiceExpandableListviewAdapter;
import cn.jksoft.businessclient.ui.adapter.WorkbenchAdapter;
import cn.jksoft.businessclient.ui.views.RecycleViewDivider;
import cn.jksoft.businessclient.utils.DialogHelp;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Author:  nick
 * Email:   nickdevp@gmail.com
 * Date:    16/04/07
 * Description:
 */
public class CreateOrderServiceFragment extends BaseFragment{
    @Bind(R.id.expandable_list)
    ExpandableListView expandableListView;

    private ProgressDialog progressDialog;

    public void setSelectServiceListener(SelectServiceListener selectServiceListener) {
        this.selectServiceListener = selectServiceListener;
    }

    private SelectServiceListener selectServiceListener;

    private ServiceExpandableListviewAdapter mAdapter;
    private List<StoreServiceEntity.StoreServiceListEntity> groupData = new ArrayList<StoreServiceEntity.StoreServiceListEntity>();
    private List<List<ProductEntity>> childData = new ArrayList<List<ProductEntity>>();

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_create_order_service, container,
                false);
        ButterKnife.bind(this, view);
        initView();
        getServiceData();
        return view;
    }

    private void initView(){
        mAdapter = new ServiceExpandableListviewAdapter(getActivity(), groupData, childData);
        expandableListView.setAdapter(mAdapter);
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                if(selectServiceListener!=null){
                    selectServiceListener.didSelectService(childData.get(groupPosition).get(childPosition));
                }
                return false;
            }
        });

        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                for (int i = 0; i < mAdapter.getGroupCount(); i++) {
                    if (i != groupPosition && expandableListView.isGroupExpanded(groupPosition)) {
                        expandableListView.collapseGroup(i);
                    }
                }
            }
        });
    }

    private void initExpandList(List<StoreServiceEntity.StoreServiceListEntity> list) {

        int size = 0;
        if (list != null && (size = list.size()) > 0) {
            for (int i = 0; i < size; i++) {
                StoreServiceEntity.StoreServiceListEntity group = list.get(i);
                groupData.add(group);
                List<ProductEntity> childList = list.get(i).getServiceList();
                childData.add(childList);
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    private void getServiceData() {

        JSONObject json = null;
        try {
            json = new JSONObject();
            json.put("storeId", BaseApplication.getInstance().getLoginEntity().getPuxsoftLoginDTO().getStoreId());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ReceivingApi service = ReceivingService.createReceivingService();
        service.getStoreServiceList(json.toString())
                .observeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<StoreServiceEntity>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(StoreServiceEntity storeServiceEntity) {

                        String retCode = storeServiceEntity.getIs_successful();
                        if ("1".equals(retCode)) {
                            initExpandList(storeServiceEntity.getStoreServiceList());
                        }
                    }
                });
    }

    public interface SelectServiceListener {
        public void didSelectService(ProductEntity entity);
    }
}
